<style lang="less">
</style>
<template>
  <div class="">
    <Row style="margin: 10px 0px;">
      <label>试卷名称</label>
      <Input v-model="condition.testtitle" placeholder="请输入试卷名称" clearable style="width: 200px"></Input>
      <!-- <label style="margin-left: 10px;"></label>
      <Input v-model="value14" placeholder="请输入出卷人" clearable style="width: 150px"></Input>
      <label style="margin-left: 10px;">考试时间</label>
      <DatePicker type="daterange" split-panels placeholder="选择时间" style="width: 200px"></DatePicker> -->
      <span style="margin: 0px 10px;">
        <button type="button" class="ivu-btn ivu-btn-primary" @click="search($getCondition(condition))">
          <i class="ivu-icon ivu-icon-search"></i>
          <span>查询</span>
        </button>
      </span>
      <span style="margin: 0px 10px;">
        <button type="button" class="ivu-btn ivu-btn-primary" @click="addPaper()">
          <i class="ivu-icon ivu-icon-plus"></i>
          <span>新增模板</span>
        </button>
      </span>
    </Row>
    <Table :columns="columns" :data="model.rows" border></Table>
    <Row style="margin: 10px 0px;">
        <Page :total="model.count" :page-size="model.pageSize" show-elevator style="text-align: center;" @on-change="loadPage"></Page>
    </Row>
    <Modal :width="800" v-model="show" title="试卷详情" @on-visible-change="setClear">
      <Form :model="paper" :label-width="100">
        <FormItem label="考试名称">
            <Input v-model="paper.testtitle"  type="text" placeholder="请输入考试名称"></Input>
        </FormItem>
        <FormItem label="单选题">
            <label style="margin-left: 10px;">题目总数</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.single"></InputNumber>
            <label style="margin-left: 10px;">总分</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.singletotal"></InputNumber>
            <span style="margin-left: 10px;">每道题&nbsp;&nbsp;
              {{isInteger(paper.singletotal/paper.single) ? paper.singletotal/paper.single : 0}}&nbsp;&nbsp;分
            </span>
        </FormItem>
        <FormItem label="单选必选">
            <group-tags v-model="paper.singlereq"></group-tags>
        </FormItem>
        <FormItem label="多选题" >
            <label style="margin-left: 10px;">题目总数</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.multiple"></InputNumber>
            <label style="margin-left: 10px;">总分</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.multipletotal"></InputNumber>
            <span style="margin-left: 10px;">每道题&nbsp;&nbsp;
              {{isInteger(paper.multipletotal/paper.multiple) ? paper.multipletotal/paper.multiple : 0}}&nbsp;&nbsp;分
            </span>
        </FormItem>
        <FormItem label="多选必选">
            <group-tags v-model="paper.multiplereq"></group-tags>
        </FormItem>
        <FormItem label="判断题">
            <label style="margin-left: 10px;">题目总数</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.judgment"></InputNumber>
            <label style="margin-left: 10px;">总分</label>
            <InputNumber :max="100" :min="0" :step="5" v-model="paper.judgmenttotal"></InputNumber>
            <span style="margin-left: 10px;">每道题&nbsp;&nbsp;
              {{isInteger(paper.judgmenttotal/paper.judgment) ? paper.judgmenttotal/paper.judgment : 0}}&nbsp;&nbsp;分
            </span>
        </FormItem>
        <FormItem label="判断必选">
            <group-tags v-model="paper.judgmentreq"></group-tags>
        </FormItem>
        <FormItem label="试卷总分">
            <span>{{getSum(paper.singletotal, paper.multipletotal, paper.judgmenttotal)}}</span>
        </FormItem>
        <FormItem label="考试时长">
           <Input type="number" v-model="paper.testtimelong" style="width: 200px;"></Input>&nbsp;&nbsp;&nbsp;分钟
        </FormItem>
        <FormItem label="考试注意事项">
          <Input v-model="paper.testinfomation"  type="textarea" :rows="4" placeholder="输入本次考试注意事项..."></Input>
        </FormItem>

      </Form>
      <div slot="footer">
        <Button type="primary" size="large" long  @click="confirm(paper.type)">确定</Button>
      </div>
    </Modal>
    <Spin size="large" fix v-if="model.state === '查询'"></Spin>
  </div>

</template>
<script>
import paperPreview from '../preview/paper-preview.vue'
import groupTags from '../self/common/group-tags.vue'
import PagedList from '@/plugins/PagedList.js'
import axios from 'axios'

    export default {
      title: '模拟题模板参数',
      name: 'simu-params',
        data () {
            return {
              test: [],
              condition: {
                testtitle: ''
              },
              model: new PagedList('rs/sql/singleTable_OrderBy', 20,
              {items: "'*'", tablename: "'testmodel'", orderitem: "'id'"}),
              show: false,
              paper: {
                testtitle: '', // 试卷名称
                single: 0, // 单选题个数
                singletotal: 0,
                singleaver: 0,
                multiple: 0, // 多选题
                multipletotal: 0,
                multipleaver: 0,
                testtimelong: 60, //考试时长
                judgment: 0, // 判断题
                judgmenttotal: 0,
                judgmentaver: 0,
                testnum: 0, // 试卷总分
                testinfomation: '', // 考试注意事项
                singlereq: [], // 单选必选
                multiplereq: [], // 多选必选
                judgmentreq: [], // 判断必选
              },
                columns: [
                    {
                      title: '序号',
                      type: 'index',
                      align: 'center',
                      width: 80
                    },
                    {
                        title: '试卷名称',
                        key: 'testtitle'
                    },
                    {
                        title: '总分',
                        key: 'testnum',
                        align: 'center',
                        width: 70
                    },
                    {
                        title: '试卷类型',
                        key: 'testtype',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '试卷总数',
                        key: 'testsum',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '单选必选题',
                        key: 'onesum',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '多选必选题',
                        key: 'doutsum',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '判断必选题',
                        key: 'judjsum',
                        align: 'center',
                        width: 100
                    },
                    // {
                    //     title: '比选题',
                    //     render: (h, params) => {
                    //       return h('p', this.getStr2Arr())
                    //     }
                    // },
                    {
                        title: '操作',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                          return h('div', [
                              h('Button', {
                                  props: {
                                      type: 'primary'
                                  },
                                  style: {
                                      marginRight: '5px'
                                  },
                                  on: {
                                      click: () => {
                                        this.modifyRow(params.row)
                                      }
                                  }
                              }, '修改'),
                              h('Button', {
                                  props: {
                                      type: 'primary'
                                  },
                                  on: {
                                      click: () => {
                                        this.deleteRow(params.row)
                                      }
                                  }
                              }, '删除'),
                          ]);
                        }
                    }
                ],
                data1: [
                    {
                        testname: '试卷名称',
                        testtitle: '试卷标题',
                        testnum: 100,
                        testtype: '综合试题',
                        testtimelong: '60分钟',
                        teststatus: '未开考'
                    }
                ],
                roles: [
                    {
                        value: '',
                        label: '全部'
                    },
                    {
                        value: '考生',
                        label: '考生'
                    },
                    {
                        value: '老师',
                        label: '老师'
                    },
                    {
                        value: '管理员',
                        label: '管理员'
                    }
                ]
            };
        },
        mounted () {
          this.search('1=1')
        },
        methods: {
            search (condition) {
              this.model.search(condition)
            },
            loadPage (index) {
                // console.log(`加载第${index}页数据`);
                this.model.loadPage(index)
            },
            addPaper () {
              this.show = true
            },
            getMinute (start, end) {
              let m = new Date(end).getTime() - new Date(start).getTime()
              return m/1000/60
            },
            confirm (str) {
              this.model.state = '查询'

              let obj = {
                // 试卷本身
                testtitle: this.paper.testtitle,
                testnum: parseInt(this.paper.singletotal) + parseInt(this.paper.multipletotal) + parseInt(this.paper.judgmenttotal),
                testtimelong: this.paper.testtimelong,
                testinfomation: this.paper.testinfomation,
                testtype: this.getType(this.paper.single, this.paper.multiple, this.paper.judgment),
                testflag: this.paper.testflag,
                // 关联表
                single: this.paper.single, // 单选题个数
                singletotal: this.paper.singletotal,
                singleaver: this.paper.singletotal/this.paper.single,
                singlereq: this.paper.singlereq,
                onesum: this.gettotal(this.paper.singlereq),
                multiple: this.paper.multiple, // 多选题
                multipletotal: this.paper.multipletotal,
                multipleaver: this.paper.multipletotal/this.paper.multiple,
                multiplereq: this.paper.multiplereq,
                doutsum: this.gettotal(this.paper.multiplereq),
                judgment: this.paper.judgment, // 判断题
                judgmenttotal: this.paper.judgmenttotal,
                judgmentaver: this.paper.judgmenttotal/this.paper.judgment,
                judgmentreq: this.paper.judgmentreq,
                judjsum: this.gettotal(this.paper.judgmentreq),
                mustmodelnum: this.gettotal(this.paper.singlereq) + this.gettotal(this.paper.multiplereq) + this.gettotal(this.paper.judgmentreq),
                testsum: parseInt(this.paper.single) + parseInt(this.paper.multiple) + parseInt(this.paper.judgment),
                // 创建人信息
                job: this.$store.state.user.userinfo.job,
                unit: this.$store.state.user.userinfo.unit,
                policeclass: this.$store.state.user.userinfo.policeclass,
                creattestpp: this.$store.state.user.userinfo.username,
              }
              if (this.paper.id) {
                obj.id = this.paper.id
              }
              if (obj.onesum > obj.single || obj.doutsum > obj.multiple || obj.judjsum > obj.judgment) {
                this.$Modal.confirm({title: '必选题数量不匹配', content: '必选题数量不能大于题目总数，请注意修改！！！'})
                this.model.state = '正确'
                return
              }
              if (!(this.isInteger(obj.singleaver) || obj.single === 0) ||
                  !(this.isInteger(obj.multipleaver) || obj.multiple === 0) ||
                  !(this.isInteger(obj.judgmentaver) || obj.judgment === 0)) {
                this.$Modal.confirm({title: '分数不匹配', content: '题目数量与总分不匹配，请注意查看！！！'})
                this.model.state = '正确'
                return
              }
              // 调取保存模板的逻辑
              // console.log('11111111111111111', obj);
              // return
              axios.post('rs/logic/savemodel', {data: obj}).then(res => {
                this.show = false
                if (res.data.code === 200) {
                  this.model.refresh()
                }
                else if (res.data.code === 201) {
                  this.$Message.error('有未考试卷，不能添加新的试卷');
                }
                this.model.refresh()
              }).catch(err => {
                this.model.state = '正确'
                this.$Message.error('试卷添加失败，请联系技术人员查看');
              })
              // this.$router.push({
              //     name: 'paperit',
              //     query: this.paper
              // });
              // this.show = false
            },
            clearObj () {
              this.paper = {
                testtitle: '', // 试卷名称
                testflag: '0', // 是否可以实时查看试卷
                single: 0, // 单选题个数
                singletotal: 0,
                singleaver: 0,
                multiple: 0, // 多选题
                multipletotal: 0,
                multipleaver: 0,
                judgment: 0, // 判断题
                judgmenttotal: 0,
                judgmentaver: 0,
                testnum: 0, // 试卷总分
                timeing: '', // 考试时间
                testlostlong: 0, // 最迟登陆时间
                testinfomation: '' // 考试注意事项
              }
            },
            setClear (val) {
              if (!val) {
                this.clearObj()
              }
            },
            getSum (one, two, three) {
              return parseInt(one) + parseInt(two) + parseInt(three)
            },
            isInteger (obj) {
              return typeof obj === 'number' && obj%1 === 0;      //是整数，则返回true，否则返回false
            },
            // 试卷类型
            getType (s, m, j) {
              let type = 0;
              if (s !== 0) {
                type += 1
              }
              if (m !== 0) {
                type += 1
              }
              if (j !== 0) {
                type += 1
              }
              return type > 1 ? '综合试题' : '单一试题'
            },
            modifyRow (row) {
              axios.post('rs/logic/getmodellist', {data: {id: row.id}}).then(res => {
                // console.log('11111111111111', res);
                // this.search("1=1")
                this.paper = Object.assign({}, res.data)
                this.show = true
              })
            },
            deleteRow (row) {  // deletetest
              axios.post('rs/logic/delmodellist', {data: {id: row.id}}).then(res => {
                // console.log('11111111111111', res);
                this.search("1=1")
              })
            },
            getStr2Arr (arr) {
              let str = ''
              arr.forEach(item => {
                str += `${item.name}:${item.num}`
              })
              return str
            },
            gettotal (arr) {
              // console.log('2222222222', arr);
              let total = 0
              arr.forEach(item => {
                total += parseInt(item.num)
              })
              return total
            }
        },
        components: {
            paperPreview,
            groupTags
        },
    };
</script>
